package test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;

public class ServletDemo9 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpSession = req.getSession(true);
        Integer count = 1;
        if(httpSession.isNew()){
            //新用户，把count值写入session中
            httpSession.setAttribute("count",count);
        }else{
            //老用户，读取count值
            count=(Integer) httpSession.getAttribute("count");
            count= count+1;
            httpSession.setAttribute("count",count);
        }
        //返回响应页面
        resp.setContentType("text/html;charset=utf-8");
        Writer writer = resp.getWriter();
        writer.write("<html>");
        writer.write("count="+count);
        writer.write("</html>");
    }
}
